package net.asfun.jvalog.action;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.domain.Category;
import net.asfun.jvalog.domain.Post;
import net.asfun.jvalog.http.RequestUtil;
import net.asfun.jvalog.misc.Formater;
import net.asfun.jvalog.view.PagerInfo;

public class PostAction extends BaseAction{
	
	public void index(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int page = RequestUtil.getIntParam(req, "page");
		PagerInfo pager = new PagerInfo();
		pager.setIndex(page);
		this.assign("posts", Post.list(pager));
		this.assign("pager", pager);
		this.render("admin/posts.html", resp);
	}
	
	public void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		this.assign("categories", Category.listAll());
		this.render("admin/post.html", resp);
	}
	
	public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String title = RequestUtil.getRequiredParam(req, "title");
		String slogan =  RequestUtil.getStringParam(req, "slogan", title);
		String article = RequestUtil.getRequiredParam(req, "article");
		String tagnames = RequestUtil.getStringParam(req, "tagNames");
		String excerpt = RequestUtil.getStringParam(req, "excerpt");
		String source = RequestUtil.getStringParam(req, "source");
		String publish = RequestUtil.getStringParam(req, "publish");
		String date = RequestUtil.getStringParam(req, "date");
		Long[] categoryIds = RequestUtil.getLongParams(req, "categories");
		Post post = new Post();
		post.setTitle(title);
		post.setSlogan(slogan);
		post.setArticle(article);
		post.setExcerpt(excerpt);
		post.setSource(source);
		try {
			if ( date.length() == 0 ) {
				post.setDate(new Date());
			} else {
				post.setDate(parseDate(date));
			}
		} catch (Exception e) {
			post.setDate(new Date());
		}
		if ( publish.length() > 0 ) {
			post.setPublished(true);
		}
		post.setTagNames(tagnames);
		post.setCategories(categoryIds);
		post.save();
		this.feedback("post.add", req, resp);
	}
	
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long[] ids = RequestUtil.getLongParams(req, "id");
		for(int i=0; i<ids.length; i++) {
			Post.remove(ids[i]);
		}
		this.feedback("post.delete", req, resp);
	}
	
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		String title = RequestUtil.getRequiredParam(req, "title");
		String slogan =  RequestUtil.getStringParam(req, "slogan", title);
		String article = RequestUtil.getStringParam(req, "article");
		String tagnames = RequestUtil.getStringParam(req, "tagNames");
		String excerpt = RequestUtil.getStringParam(req, "excerpt");
		String source = RequestUtil.getStringParam(req, "source");
		String publish = RequestUtil.getStringParam(req, "publish");
		String date = RequestUtil.getStringParam(req, "date");
		Long[] categoryIds = RequestUtil.getLongParams(req, "categories");
		Post post = Post.load(id);
		post.setTitle(title);
		post.setSlogan(slogan);
		if ( article.length() > 0 ) {
			post.setArticle(article);
		}
		try {
			post.setDate(parseDate(date));
		} catch (Exception e) {/*do not change*/}
		post.setExcerpt(excerpt);
		post.setSource(source);
		if ( publish.length() > 0 ) {
			post.setPublished(true);
		} else {
			post.setPublished(false);
		}
		post.setTagNames(tagnames);
		post.setCategories(categoryIds);
		post.update();
		this.feedback("post.update", req, resp);
	}
	
	public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		this.assign("post", Post.load(id));
		this.assign("categories", Category.listAll());
		this.render("admin/post.html", resp);
	}
	
	public void publish(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Long id = RequestUtil.getLongParam(req, "id");
		this.assign("post", Post.load(id));
		this.assign("categories", Category.listAll());
		this.render("admin/publish.html", resp);
	}
	
	private Date parseDate(String date) {
		int len = date.length();
		if (len == 10 ) {
			date += Formater.date2string(new Date(), " hh:mm:ss");
		}
		if (len == 13 ) {
			date += Formater.date2string(new Date(), ":mm:ss");
		}
		if (len == 16 ) {
			date += ":00";
		}
		return Formater.string2date(date, "yyyy-MM-dd HH:mm:ss");
	}
}
